<?php
session_start(); // start PHP session

require_once "config.php"; // use a config file
// require_once "AlaTracConverter.php"; // use the original trac-style-wiki-formatter
require_once "AlaTracConverterRedux.php"; // use the redux trac-style-wiki-formatter

// get the file to translate, if nothing was passed
$file = $_GET ? $wikidir . "/" . $_GET['f'] . ".wiki" : $wikidir . '/test.wiki';
$doc_title = $_GET['f'] ? $_GET['f'] : 'test';
 
// otherwise, create xslt
$xsldoc = new DOMDocument();
$xsl = new XSLTProcessor();
$xsldoc->load('mywiki.xsl');

// import the stylesheet as an xml document
$xsl->importStyleSheet($xsldoc);

if (!file_exists($file)) {
    // create new file and notify that file will not be saved automatically, i.e. press update if want to save
    $doc_text = ''; // set to nothing for now
    $xmldoc = new DOMDocument(); // empty for now
    $warnings = "The file $file does not exist - type and press 'update' to save it.";
}
else
{
    // create the AlaTracConverter object
    $atc = new AlaTracConverter($file);         

    // create a dom doc object for the scanned xml wiki
    $xmldoc = new DOMDocument();
    // die($atc->xhtml);
    $xmldoc->loadXML($atc->xhtml); // load the xml from output

    $doc_text = file_get_contents($file);
    $warnings = false;
}

if ($_SESSION['user'] == 'admin') {
    // set the xslt parameters
    $xsl->setParameter('', array('doc_title' => $doc_title, 'doc_text' => $doc_text, 'warnings' => $warnings, 'admin_view' => 'true'));
}
else
{
    // set the xslt parameters
    $xsl->setParameter('', array('doc_title' => $doc_title, 'doc_text' => $doc_text, 'warnings' => $warnings, 'admin_view' => 'false'));
}
// header('Content-type: text/html');
echo $xsl->transformToXML($xmldoc); // print it out
